<template>
  <div class="app-container">
    <!--    表单查询区-->
    <div class="filter-container">
      <div class="filter-item" style="margin-right: 5px;">课程名：</div>
      <el-select v-model="listQuery.id" class="filter-item" filterable placeholder="请选择" style="margin-right: 10px;" @change="getTimeListByCourseId">
        <el-option
          v-for="item in courseList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <div class="filter-item" style="margin-right: 5px;">课时：</div>
      <el-select v-model="listQuery.classTimeId" class="filter-item" filterable placeholder="请选择" style="margin-right: 10px;" @change="getClassTimePage">
        <el-option
          v-for="item in classTimeList"
          :key="item.id"
          :label="item.title"
          :value="item.id"
        />
      </el-select>
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        导出
      </el-button>
    </div>

    <!--表格区-->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" type="index" width="60px" align="center" />
      <el-table-column label="学员姓名" prop="realName" align="center" />
      <el-table-column label="联系方式" prop="phone" align="center" />
      <el-table-column label="预约时间" align="center">
        <template v-slot="{row}">
          {{ parseTime(row.experienceTime, '{y}年{m}月{d}日 {h}:{m}:{s}') }}
        </template>
      </el-table-column>
    </el-table>

    <!--分页插件-->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getClassTimePage" />

  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'
import { getCourseListByRole } from '@/api/course'
import { getTimeByCourseId, getTimeList, getTimePage } from '@/api/course-time'

export default {
  name: 'ClassReserve',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: [],
      listQuery: {
        key: '',
        current: 1,
        size: 10,
        // 选择器绑定课程id
        id: undefined,
        // 课时id
        classTimeId: undefined
      },
      listLoading: false,
      courseList: [],
      // 评价总条数
      total: 0,
      downloadLoading: false,
      // 课程课时列表
      classTimeList: []
    }
  },
  created() {
    this.getCourseList()
  },
  methods: {
    parseTime,
    // 根据用户角色获取课程列表
    getCourseList() {
      getCourseListByRole().then(res => {
        this.courseList = res.data
        if (this.courseList <= 0) {
          this.courseList.unshift({
            id: 0,
            name: '暂无课程'
          })
          this.listQuery.id = 0
        } else {
          this.listQuery.id = res.data[0].id
          this.getTimeListByCourseId()
        }
      })
    },
    // 根据课程id获取课程列表
    getTimeListByCourseId() {
      getTimeByCourseId(this.listQuery.id).then(res => {
        this.classTimeList = res.data
        if (this.classTimeList <= 0) {
          this.classTimeList.unshift({
            id: 0,
            title: '暂无课时信息'
          })
          this.listQuery.classTimeId = 0
        } else {
          this.listQuery.classTimeId = res.data[0].id
        }
      }).catch(() => {
        this.classTimeList = []
        this.classTimeList.unshift({
          id: 0,
          title: '暂无课时信息'
        })
        this.listQuery.classTimeId = 0
      })
    },
    // 根据课程id和课时id获取课时预约列表
    getClassTimePage() {
      getTimePage(this.listQuery).then(res => {
        this.list = res.data.records
        this.total = res.data.total
      }).catch(() => {
        this.list = []
        this.total = 0
      })
    },
    // 导出函数
    handleDownload() {
      this.downloadLoading = true
      let courseName = ''
      this.courseList.forEach(course => {
        if (course.id === this.listQuery.id) {
          courseName = course.name
        }
      })
      let classTimeName = ''
      this.classTimeList.forEach(classTime => {
        if (classTime.id === this.listQuery.classTimeId) {
          classTimeName = classTime.title
        }
      })
      getTimeList(this.listQuery).then(res => {
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['序号', '学员姓名', '手机号码', '预约时间']
          const filterVal = ['id', 'realName', 'phone', 'experienceTime']
          const data = this.formatJson(filterVal, res.data)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: courseName + '_' + classTimeName + '_预约名单' + parseTime(new Date(), '{y}-{m}-{d}-{h}-{i}-{s}')
          })
          this.downloadLoading = false
        })
      }).catch(() => {
        this.downloadLoading = false
      })
    },
    // 格式化下载数据
    formatJson(filterVal, list) {
      return list.map(v => filterVal.map(j => {
        if (j === 'experienceTime') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>

<style scoped>

</style>
